﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace FileTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //using (HashAlgorithm hash = HashAlgorithm.Create())
            //{
            //    using (FileStream file1 = new FileStream(@"E:\c#练习\WebStorage\WebStorage\Upload\e284c4e8-7dc6-4a67-8e08-4d2cf30da2c3.zip", FileMode.Open), file2 = new FileStream(@"D:\360安全浏览器下载\1123_154730_vmsecadapterlog.zip", FileMode.Open))
            //    {
            //        byte[] hashByte1 = hash.ComputeHash(file1);//哈希算法根据文本得到哈希码的字节数组 
            //        byte[] hashByte2 = hash.ComputeHash(file2);
            //        string str1 = BitConverter.ToString(hashByte1);//将字节数组装换为字符串 
            //        string str2 = BitConverter.ToString(hashByte2);
            //        Console.WriteLine(str1 == str2);//比较哈希码 
            //    }
            //}

            var res = GetMD5HashFromFile(@"D:\360安全浏览器下载\1123_154730_vmsecadapterlog.zip");
            Console.WriteLine(res);
            Console.ReadLine();
        }

        private static string GetMD5HashFromFile(string fileName)
        {
            try
            {
                FileStream file = new FileStream(fileName, FileMode.Open);
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] retVal = md5.ComputeHash(file);
                file.Close();

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < retVal.Length; i++)
                {
                    sb.Append(retVal[i].ToString("x2"));
                }
                return sb.ToString();
            }
            catch (Exception ex)
            {
                throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message);
            }
        }
    }
}
